package com.szgr.baoyin.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.adapter.AddressListAdapter;
import com.szgr.baoyin.model.Address;
import com.szgr.baoyin.utils.Constant;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收货地址
 */
public class AddressActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    private AddressListAdapter adapter;
    private List<Address> data;
    private boolean fromOrder = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_address);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initActionBar() {
        actionBarTitle.setText("收货地址");
    }

    @Override
    public void initView() {
        data = new ArrayList<>();
        ListView addressListView = (ListView) findViewById(R.id.address_list_view);

        adapter = new AddressListAdapter(this,data);
        adapter.setListener(this);
        addressListView.setAdapter(adapter);
        addressListView.setOnItemClickListener(this);

        findViewById(R.id.address_add).setOnClickListener(this);

        Intent intent = getIntent();
        if(intent.hasExtra("from")){
            fromOrder = true;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getAddress();
    }

    /**
     * 获取address列表
     */
    private void getAddress() {
        StringRequest jor = new StringRequest(Request.Method.POST, Constant.URL_ALL_ADDRESS,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String jsonObject) {
                        System.out.println("------>" + jsonObject);
                        try {
                            Gson gson = new Gson();
                            JSONObject jsonObject1 = new JSONObject(jsonObject);
                            JSONObject datas = jsonObject1.getJSONObject("datas");
                            JSONArray address_list = datas.getJSONArray("address_list");
                            data = gson.fromJson(address_list.toString(), new TypeToken<List<Address>>() {
                            }.getType());
                            adapter.setData(data);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map map = new HashMap();
                map.put("key",MyApplication.getmInstance().getKey());
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(jor);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.action_bar_left:
                finish();
                break;
            case R.id.address_add:
                startActivity(new Intent(this, AddAddressActivity.class));
                break;
            case R.id.item_icon:
                int tag = (int) v.getTag();
                Address address = data.get(tag);
                Intent intent = new Intent(this,AddAddressActivity.class);
                intent.putExtra("address",address);
                startActivity(intent);
                break;
            case R.id.item_delete:
                int tag1 = (int) v.getTag();
                Address address1 = data.get(tag1);
                deleteAddress(address1.getAddress_id());
                break;
        }
    }

    private void deleteAddress(final String addressId) {
        StringRequest sr = new StringRequest(Request.Method.POST, Constant.URL_DELETE_ADDRESS,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        System.out.println("---delete--->" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            if(jsonObject.has("datas")){
                                String datas = jsonObject.getString("datas");
                                if("1".equals(datas)){
                                    getAddress();
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("address_id",addressId);
                map.put("key",MyApplication.getmInstance().getKey());
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(sr);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(fromOrder) {
            Address address = data.get(position);
            Intent intent = new Intent();
            intent.putExtra("address", address);
            setResult(304, intent);
            finish();
        }
    }
}
